/**
 * 自定义控件集合
 * 基于JQ,KENDOUI
 */
var Morinda={
	/*
	 * 控件集合
	 */
	ui:{
		message:{
			 /**
			  * 弹出提示框
			  * @param message 信息
			  */
			alert:function(message){
				//创建提示窗口的ID
				 var uid="Morinda-ui-message-alert-"+Morinda.createGUID();
				//创建提示窗口的载体
				 $("body").append("<div id='"+uid+"' style='display:none;' class='Morinda-ui-message-alert'><div>");
				 //将要提示的文字写入
				 $("#"+uid).append("<div class='panel Morinda-ui-message-alert-content'>"+message+"</div>");
				 //初始化确定按钮
				 $("#"+uid).append("<div style='width:100%;text-align:center;' id='toolbar-"+uid+"'><div id='btn-ok-"+uid+"' class='btn btn-primary ok'>确定</div></div>");
				 //将载体与kendo的window结合 
				 var dialog = $("#"+uid);
				  if (!dialog.data("kendoWindow")) {
					  dialog.kendoWindow({                             
		              title: "提示信息",
		              width:260,		 
		              height:"auto",
		              modal:true,
		              animation: false,
		              draggable: true,
		              resizable: false,
		              rootModal:true,
		              actions: [                                  
		                  "Close"
		              ] 
		          });                           
				 }
				 dialog.data("kendoWindow").center().open(); 
				 //确定按钮绑定关闭功能
				 
				 $(dialog[0].children[2].children[0]).click(function(){
					 dialog.data("kendoWindow").close();
				 });
				 
			},
			/**
			 * 确认选择提示框
			 * @param message 要显示的信息
			 * @param cb  回调函数
			 */
			confirm:function(message,cb){
				//创建提示窗口的ID
				 var uid="Morinda-ui-message-confirm-"+Morinda.createGUID();
				//创建提示窗口的载体
				 $("body").append("<div id='"+uid+"' style='display:none;' class='Morinda-ui-message-confirm'><div>");
				 //将要提示的文字写入
				 $("#"+uid).append("<div class='panel Morinda-ui-message-confirm-content'>"+message+"</div>");
				 //初始化确定按钮
				 $("#"+uid).append("<div style='width:100%;text-align:center;'><div id='btn-ok-"+uid+"' class='btn btn-primary'>确定</div>&nbsp;&nbsp;<div id='btn-cancel-"+uid+"' class='btn btn-primary'>取消</div></div>");
				 //将载体与kendo的window结合 
				 var dialog = $("#"+uid);
				  if (!dialog.data("kendoWindow")) {
					  dialog.kendoWindow({                             
		              title: "确认信息",
		              width:260,		 
		              height:"auto",
		              modal:true,
		              animation: false,
		              draggable: true,
		              resizable: false,
		              rootModal:true,
		              actions: [                                  
		                  "Close"
		              ] 
		          });                           
				 }
				 dialog.data("kendoWindow").center().open(); 
				 //确定按钮绑定关闭功能
				 $(dialog[0].children[2].children[0]).click(function(){
					 dialog.data("kendoWindow").close();
					 if(cb){
						 cb(true);
					 }		
				 });
				 //取消按钮绑定
				 $(dialog[0].children[2].children[1]).click(function(){		 
					 dialog.data("kendoWindow").close();
					 if(cb){
						 cb(false);
					 }					  
				});
			}
		},
		/**
		 * 三级联动地址控件
		 * @param config
		 * var config={
					province:{
						el:"select[name=province]",
						proxy:{
							url:"/nareaAction!getSubAddressByParentId.do",
							params:{parentId:"0"}
						} 
					},
					city:{
						el:"select[name=city]",
						proxy:{
							url:"/nareaAction!getSubAddressByParentId.do",
							params:{parentId:" "}
						} 
					},
					county:{
						el:"select[name=county]",
						proxy:{
							url:"/nareaAction!getSubAddressByParentId.do",
							params:{parentId:" "}
						} 
					},
					value:""
			}
		 */
		address:function(config){ 
			 var me={config:config};
			 var container=$(me.config.el);
			 container.append("<select name='"+config.province.name+"'   style='width:150px;' ></select>");
			 container.append("<select name='"+config.city.name+"'   style='width:150px;' ></select>");
			 container.append("<select name='"+config.county.name+"'   style='width:150px;' ></select>");
			 
			 me.config.province.el="select[name="+config.province.name+"]";
			 me.config.city.el="select[name="+config.city.name+"]";
			 me.config.county.el="select[name="+config.county.name+"]";
			//初始省数据源 
  			me.provinceDataSource = new kendo.data.DataSource({
				  transport: {					
				    read: {
				    	type: "POST", 
                 	      dataType: "jsonp",
                 	      url:me.config.province.proxy.url,
                 	      data:me.config.province.proxy.params
				    }
				  },
				  schema: {
					  data: "data"
				  }
				});
  			
  			me.province=$(me.config.province.el).kendoComboBox({
  				placeholder: "请选择",
                dataTextField: "name",
                dataValueField: "id",
                dataSource: me.provinceDataSource,                        
                select: function(e) {                     
                    var dataItem = this.dataItem(e.item.index());  
                    me.cityDataSource.options.transport.read.data.parentId=dataItem.id;				           
                    me.cityDataSource.read(); 
                    me.countyDataSource.options.transport.read.data.parentId="";				           
                	me.countyDataSource.read();
                	$(me.config.county.el).data("kendoComboBox").text("");    
                	$(me.config.city.el).data("kendoComboBox").text(""); 
                  }  
            });
  			
  			//初始市数据源 
  			me.cityDataSource = new kendo.data.DataSource({
				  transport: {					
				    read: {
				    	type: "POST", 
                 	      dataType: "jsonp",
                 	      url:me.config.city.proxy.url,
                 	      data:me.config.city.proxy.params
				    }
				  },
				  schema: {
					  data: "data"
				  }
				});
  		
  			me.city=$(me.config.city.el).kendoComboBox({
  				placeholder: "请选择",
                dataTextField: "name",
                dataValueField: "id",
                dataSource: me.cityDataSource, 
                select: function(e) {
                	var dataItem = this.dataItem(e.item.index()); 
                	me.countyDataSource.options.transport.read.data.parentId=dataItem.id;				           
                	me.countyDataSource.read();                 	   
                	$(me.config.city.el).data("kendoComboBox").text(""); 
                  } 
            }); 
  		   //初始市数据源 
  			me.countyDataSource = new kendo.data.DataSource({
				  transport: {					
				    read: {
				    	type: "POST", 
                 	      dataType: "jsonp",
                 	      url: me.config.county.proxy.url,
                 	      data:me.config.county.proxy.params
				    }
				  },
				  schema: {
					  data: "data"
				  }
				});
  		
  			me.county=$(me.config.county.el).kendoComboBox({
  				placeholder: "请选择",
                dataTextField: "name",
                dataValueField: "id", 
                dataSource: me.countyDataSource,
                select: function(e) {
                	var dataItem = this.dataItem(e.item.index()); 
                	
                  } 
            }); 
  			//取值
  			me.getValue=function(){
  				return {
  					province:{
  						value:me.province.data("kendoComboBox").value(),
  	  					text:me.province.data("kendoComboBox").text()
  					},
  					city:{
  						value:me.city.data("kendoComboBox").value(),
  	  					text:me.city.data("kendoComboBox").text()
  					},
  					county:{
  	  					value:me.county.data("kendoComboBox").value(),
  	  					text:me.county.data("kendoComboBox").text()
  	  				}
  				} 
  			} 
  			//赋值
  			me.setValue=function(value){
  				
  				if(value)
  				{
  					//给省赋值
  					if(value.province)
  					{
  						if(value.province.value)
  						{
  							me.province.data("kendoComboBox").value(value.province.value);
  						}	
  						if(value.province.text)
  						{
  							me.province.data("kendoComboBox").text(value.province.text);
  						}
  	  	  				
  					}	 
  	  				//给市赋值
  					if(value.city)
  					{
  						if(value.city.value)
  						{
  							me.city.data("kendoComboBox").value(value.city.value);
  						}	
  						if(value.city.text)
  						{
  							me.city.data("kendoComboBox").text(value.city.text);
  						}
  	  	  				
  					}
  					//给县赋值
  					if(value.county)
  					{
  						if(value.county.value)
  						{
  							me.county.data("kendoComboBox").value(value.county.value);
  						}	
  						if(value.county.text)
  						{
  							me.county.data("kendoComboBox").text(value.county.text);
  						} 
  					} 
  				}	
  				
  			}
  			//控件渲染完成的事件
  			me.onRender=function(eventHander){
  				
  			}
  			return me;		
		},		
		/**
		 * @param displayDiv(窗体div是隐藏在页面)
		 * @param w(宽度)
		 * @param h(高度)
		 * @param title(标题)
		 * 弹出model窗体
		 */ 
		showDialog:function (config){
		 		//创建提示窗口的ID
				 var uid="Morinda-ui-dialog-"+Morinda.createGUID();
				//创建提示窗口的载体
				 $("body").append("<div id='"+uid+"' style='display:none;' class='Morinda-window'><div>");
				 //将要提示的文字写入
				 $("#"+uid).append("<div class='panel-default'>"+config.content+"</div>");
				 //初始化确定按钮
				 if(config.bbarVisable)
				 {
				 	var bars="<div  class='Morinda-window-bbar'>";
			 		bars+="<input type='button' class='btn btn-primary' id='btn-ok-"+uid+"'  value='确定'/>";
			 		bars+="<input type='button' class='btn btn-primary' id='btn-cancel-"+uid+"'  value='取消'/>";
		 			bars+="</div>";		 		
				 	$("#"+uid).append(bars);
				 }
				 				 
				var dialogDiv = $("#" + uid	);
				if (!dialogDiv.data("kendoWindow")) {
		            dialogDiv.kendoWindow({                             
		                title: config.title,
		                width:config.width,
		                height:config.height,
		                modal:true,
		                animation: false,
		                draggable: false,
		                actions: [                                  
		                    "Close"
		                ] 
		            });                           
				}
			var dialog = dialogDiv.data("kendoWindow");
			dialog.title(config.title);
			dialog.center().open();
		}  
	},
	 /**
	  * 创建伪GUID
	  * @returns
	  */
	createGUID:function ()
	{
	    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	        var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
	        return v.toString(16);
	    });
	},
	/*
	 * 快捷别名集合
	 */	
	//弹出提示框
	alert:function(message){ 
	 	this.ui.message.alert(message);
	},
	//确认提示框
	confirm:function(message,cb){ 
	 	this.ui.message.confirm(message,cb);
	} 
}